<?php
defined( '_PSPEXEC_' ) or die( 'Restricted access' );
class Loader{
	public static $_requestparams = array();

	public static $_classMap = array();
	
	public static $_loaderMap = array();

	public static $search = false;
	
	public static $searchLib = array('lib','models','datasource','behaviors','controllers','components','config','views','helpers','vendors','events','locales');	

	public static $filetypes = array(
		'models' => '_model',
		'behaviors' => '_behavior',
		'controllers' => '_controller',
		'components' => '_component',
		'helpers' => '_helper',
		'events' => '_event',
	);	
	
	public static $plugins_root = null;
	
	public static $framework_plugins_root = null;

	public static $cur_plugins_root = null;	

	public static $framework_root = null;

	public static $app_root = null;	
	

	public static function Instance() {
	    self::$_requestparams = Request::$params;
		self::build();
	}

	public static function build($type=null,$rest=false) {
		if (!self::$search || $rest) {
			self::$search = array(); 
			self::$framework_root = FRAMEWORK_ROOT  . DS . 'lib' ;							
			self::$app_root = APP_ROOT  . DS . 'app' ;
			
            if(isset(self::$_requestparams['plugin']) && !empty(self::$_requestparams['plugin'])) {
			    self::$plugins_root = APP_ROOT . Param::read('plugins.path')  . DS . self::$_requestparams['plugin'];	
			    self::$framework_plugins_root = FRAMEWORK_ROOT  . DS . 'plugins'  . DS . self::$_requestparams['plugin'];	
                if( is_dir(self::$plugins_root) && file_exists(self::$plugins_root) ) {
				    self::$cur_plugins_root = self::$plugins_root;			
				}else if( is_dir(self::$framework_plugins_root ) && file_exists(self::$framework_plugins_root ) ) {	
				    self::$cur_plugins_root = self::$framework_plugins_root;					
				}
			    $paths['lib'][] = self::$cur_plugins_root . DS . 'lib';
			    $paths['models'][] = self::$cur_plugins_root . DS . 'models';		
			    $paths['datasource'][] = self::$cur_plugins_root . DS . 'datasource';
			    $paths['behaviors'][] = self::$cur_plugins_root . DS . 'behaviors';
				$paths['controllers'][] = self::$cur_plugins_root . DS . 'controllers';				
				$paths['components'][] = self::$cur_plugins_root . DS . 'components';
				$paths['config'][] = self::$cur_plugins_root . DS . 'config';				
			    $paths['views'][] = self::$cur_plugins_root . DS .  'views';
			    $paths['helpers'][] = self::$cur_plugins_root . DS . 'helpers';
			    $paths['vendors'][] = self::$cur_plugins_root . DS . 'vendors';
			    $paths['events'][] = self::$cur_plugins_root . DS . 'events';
			    $paths['locales'][] = self::$cur_plugins_root . DS . 'locales';				
			}
			$paths['lib'][] = self::$app_root . DS . 'lib';
			$paths['models'][] = self::$app_root . DS . 'models';		
			$paths['datasource'][] = self::$app_root . DS . 'datasource';
			$paths['behaviors'][] = self::$app_root . DS . 'behaviors';
		    $paths['controllers'][] = self::$app_root . DS . 'controllers';				
		    $paths['components'][] = self::$app_root . DS . 'components';
			$paths['config'][] = self::$app_root . DS . 'config';				
			$paths['views'][] = APP_ROOT . Param::read('View.path');
			$paths['helpers'][] = self::$app_root . DS . 'helpers';
			$paths['vendors'][] = self::$app_root . DS . 'vendors';
			$paths['events'][] = self::$app_root . DS . 'events';
			$paths['locales'][] = self::$app_root . DS . 'locales';
			
			$paths['models'][] = self::$framework_root . DS . 'models';
			$paths['datasource'][] = self::$framework_root . DS . 'datasource';				
			$paths['behaviors'][] = self::$framework_root . DS . 'behaviors';
		    $paths['controllers'][] = self::$framework_root . DS . 'controllers';				
			$paths['components'][] = self::$framework_root . DS . 'components';
			$paths['config'][] = FRAMEWORK_ROOT  . DS  . 'config';				
			$paths['views'][] = self::$framework_root . DS .  'views';
			$paths['helpers'][] = self::$framework_root . DS . 'helpers';
			$paths['vendors'][] = FRAMEWORK_ROOT . DS . 'vendors';
			$paths['events'][] = self::$framework_root . DS . 'events';
			self::$search = $paths;			
		}
		if ($type && isset(self::$search[$type])) {
			return self::$search[$type];
		}
		return self::$search;		
	}
	
	public static function uses($className, $location=null) {	    
	    if( empty($className ) ) {
            return null ; 
        }
		if( empty($location) ) {
		    $location = self::explodedir($className); 
		}		
		if($location) {
            self::$_loaderMap[$location] = true;		
		    self::$_classMap[$className] = $location;		
		}
	}	
	
	public static function explodedir($filePath) {	
		if (!isset($filePath)) {
			return NULL ;
		}
	    if( isset(self::$_classMap[$filePath]) && !empty( self::$_classMap[$filePath] ) ) {
            return self::$_classMap[$filePath] ; 
        }  		
		if (strpos($filePath, '.') !== false) {
			$names = explode('.', $filePath, 8);
			$var = $names[0];
		}
		$paths = array();
		switch (count($names)) {
			case 2:
			    /*$names[1] = ucfirst($names[1]);*/
				if ($var == 'pspframework') {
					$paths[] = FRAMEWORK_ROOT ;
					$paths[] = FRAMEWORK_ROOT . DS . 'lib';
				}else if ($var == 'app') {
				    $paths[] = APP_ROOT . DS . 'app' ;
				}else if ($var == 'plugins') {				
				    $paths[] = self::$cur_plugins_root ;
				}else if ( in_array( $var ,self::$searchLib) ) {
				    $paths = self::build($var);
				}
				$filename = $names[1];
				foreach ($paths as $path) {
				    $names[1] = $filename;
				    if( in_array( $var ,self::$searchLib) ) {
				        if($var == 'controllers' && isset(Request::$params['prefixes']) && !empty(Request::$params['prefixes']) ) {
					        $names[1] = Request::$params['prefixes'].'_'.$names[1];	
					    }
						if(isset(self::$filetypes[$var])) {
						    $names[1] = $names[1] .self::$filetypes[$var];
						}                       						
					}
					if( file_exists($path . DS . $names[1] . '.php' ) ) {					
				        return $path . DS . $names[1] . '.php' ;
				    }						
	
				}
			break;
			case 3:
			    /*$names[2] = ucfirst($names[2]);*/
				if ($var == 'pspframework') {
					$paths[] = FRAMEWORK_ROOT . DS . 'lib'. DS .$names[1] . DS . $names[2] . '.php';
				}else if ($var == 'app') {
				    $paths[] = APP_ROOT . DS . 'app' . DS  . $names[1] . DS . $names[2]  . '.php';
				}else if ($var == 'plugins') {				
				    $paths[] = self::$cur_plugins_root . DS  . $names[1] . DS . $names[2] . '.php';
				}else if ( in_array( $var ,self::$searchLib) ) {
				    $paths = self::build($var);
					foreach ($paths as $key => $path) {
					    $paths[$key] = $path . DS  . $names[1] . DS . $names[2] . '.php';
					}
				}				
				foreach ($paths as $path) {
					if( file_exists($path ) ) {
				        return $path ;
				    }			
				}
			break;
			default:
 				if (count($names)>=4) {
				    $namepath = '';
                    unset($names[0]);
                    $namepath = trim(implode( DS ,$names),DS);					
				    if ($var == 'pspframework') {
					    $paths[] = FRAMEWORK_ROOT . DS . 'lib'. DS .$namepath . '.php';
				    }else if ($var == 'app') {
				        $paths[] = APP_ROOT . DS . 'app' . DS  . $namepath  . '.php';
				    }else if ($var == 'plugins') {				
				        $paths[] = self::$cur_plugins_root . DS  . $namepath . '.php';
				    }else if ( in_array( $var ,self::$searchLib) ) {
				        $paths = self::build($var);
					    foreach ($paths as $key => $path) {
					        $paths[$key] = $path . DS  . $namepath . '.php';
					    }
				    }				
				    foreach ($paths as $path) {
					    if( file_exists($path ) ) {
				            return $path ;
				        }			
				    }				
				} 						        
			break;				
		}
        return NULL ;		
	}	
	
/*
pspframework.controllers.posts
app.controllers.posts
app.appconrollers
models.posts
plugins.controllers.posts
*/						

	public static function import( $classpath = null, $filepath = null )
	{
	    if($filepath && !isset(self::$_loaderMap[$filepath]) ) {
		    include($filepath);
            self::$_loaderMap[$filepath] = true;
            return true;			
		}
	    $path = self::explodedir($classpath);
		if ( $path && !isset(self::$_loaderMap[$path]) ) {
			include($path);
		    self::uses($classpath, $path);
			return true;
		}
		return false;
	}
			
}	

Loader::Instance();

?>